<?php

namespace App\Http\Controllers\Admin;

use App\Models\Deposit;
use App\Models\Oncustomer;
use App\Models\Onorder;
use App\Models\Order;
use App\Models\Reorder;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        view()->share([
            '_orders' => 'am-in',
            '_order' => 'am-active',

        ]);

    }


//订单管理首页
    public function index(Request $request)
    {
        //搜索
        $where = function ($query) use ($request) {
           //会员姓名
            if ($request->has('oncustomer_name') and $request->oncustomer_name != '') {
                $search = "%" . $request->oncustomer_name . "%";
                $query->where('oncustomer_name', 'like', $search);
            }
            //按会员电话
            if ($request->has('oncustomer_tel') and $request->oncustomer_tel != '') {
                $search = "%" . $request->oncustomer_tel . "%";
                $query->where('oncustomer_tel', 'like', $search);
            }
            //按订单号
            if ($request->has('order_no') and $request->order_no != '') {
                $search = "%" . $request->order_no . "%";
                $query->where('order_no', 'like', $search);
            }
            //按门店
            if ($request->has('store_id') and $request->store_id != '-1') {

                $query->where('store_id', $request->store_id);
            }

            if ($request->has('pay_way') and $request->pay_way != '-1') {

                $query->where('pay_way', $request->pay_way);
            }
            if ($request->has('type') and $request->type != '-1') {

                $query->where('type', $request->type);
            }
            if ($request->has('company_id') and $request->company_id != '') {
                $oncustomer_id=Oncustomer::where('company_id',$request->company_id)->pluck('id')->toarray();
                $query->whereIn('oncustomer_id', $oncustomer_id);
            }
            if ($request->has('oncustomer_id') and $request->oncustomer_id != '') {

                $query->where('oncustomer_id', $request->oncustomer_id);
            }



        };
        //所有门店
        $cates = Store::all();
        //订单关联会员关联门店
        $orders = Order::where($where)->where('is_pay',1)->with('oncustomer')->with('store')->orderBy('pay_at', 'desc')->paginate(20);

        return view('admin.order.order', compact('orders', 'cates'));
    }


}
